package com.tongcheng.controller;

import com.tongcheng.ResultDate;
import com.tongcheng.commons.Contants;
import com.tongcheng.pojo.Goods;
import com.tongcheng.service.GoodsService;
import io.swagger.annotations.*;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ClassName: GoodsController
 * Description: TODO
 * Author: lcl
 * Date: 2020/11/26 14:23
 * Version: 1.0
 */
@Controller
@RequestMapping("/goods")
@Api(value = "商品",tags = "商品API")
public class GoodsController {

    @Autowired(required = false)
    private GoodsService goodsService;

    @RequestMapping(value = "/queryGoods",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品列表查询",notes = "根据商家id或商品id或商品名称或商品状态查询对应的商品信息(不传参数就查询所有商品)",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchant_id", value = "商家id",required = false),
            @ApiImplicitParam(name = "goods_id", value = "商品id",required = false),
            @ApiImplicitParam(name = "goods_name", value = "商品名称",required = false),
            @ApiImplicitParam(name = "goods_shelves_id", value = "商品状态id",required = false)
    })
    @ResponseBody
    public ResultDate queryGoods(Integer merchant_id,Integer goods_id,String goods_name,Integer goods_shelves_id){
        ResultDate resultDate = new ResultDate();
        System.out.println("查询对应的商品信息" + merchant_id + " " + goods_id + " " + goods_name + " " + goods_shelves_id);
        List<Map> mapList = goodsService.queryGoods(merchant_id,goods_id,goods_name,goods_shelves_id);
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else if (mapList.size() == 0){
            resultDate.setCode(Contants.NOT_NO);
            resultDate.setMsg("暂无满足条件数据");
        } else {
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统错误，请联系管理员");
        }
        return resultDate;
    }

    @RequestMapping(value = "/queryGoodsShelves",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品状态信息查询",notes = "商品状态信息查询",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ResponseBody
    public ResultDate queryGoodsShelves(){
        ResultDate resultDate = new ResultDate();
        System.out.println("查询商品状态名称信息");
        List<Map> mapList = goodsService.queryGoodsShelves();
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/queryGoodById",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品详细信息查询",notes = "根据商品id查询对应的商品详细信息",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id",required = true,paramType = "query",dataType = "Integer")
    })
    @ResponseBody
    public ResultDate queryGoodById(Integer goods_id){
        ResultDate resultDate = new ResultDate();
        System.out.println("根据商品id查询对应的商品详细信息" + " " + goods_id);
        List<Map> mapList = goodsService.queryGoodById(goods_id);
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/querySellLikeGoods",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询热销商品(app端接口)",notes = "热销商品查询接口",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ResponseBody
    public ResultDate querySellLikeGoods(){
        ResultDate resultDate = new ResultDate();
        System.out.println("查询热销商品");
        List<Map> mapList = goodsService.querySellLikeGoods();
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/queryGroupBookGoods",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询参加团购的所有商品(app端接口)",notes = "可根据商品名称或店铺名称实现模糊查询",httpMethod = "GET", response = ResultDate.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchanr_goods_name", value = "商家名称或者商品名称",required = false,dataType = "String",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ResponseBody
    public ResultDate queryGroupBookGoods(String merchanr_goods_name){
        ResultDate resultDate = new ResultDate();
        System.out.println("查询所有参加团购商品");
        List<Map> mapList = goodsService.queryGroupBookGoods(merchanr_goods_name);
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/queryGoodsApp",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询所有商品列表接口(app端接口)",notes = "可根据商品名称或店铺名称实现模糊查询",httpMethod = "GET", response = ResultDate.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchanr_goods_name", value = "商家名称或者商品名称",required = false,dataType = "String",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ResponseBody
    public ResultDate queryGoodsApp(String merchanr_goods_name){
        ResultDate resultDate = new ResultDate();
        System.out.println("查询所有商品列表接口(可根据商品名称或店铺名称实现模糊查询)" + merchanr_goods_name);
        List<Map> mapList = goodsService.queryGoodsApp(merchanr_goods_name);
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/queryGoodByIdApp",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品详细信息查询(app端接口)",notes = "根据商品id查询对应的商品详细信息",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id",required = true,paramType = "query",dataType = "Integer")
    })
    @ResponseBody
    public ResultDate queryGoodByIdApp(Integer goods_id){
        ResultDate resultDate = new ResultDate();
        System.out.println("根据商品id查询对应的商品详细信息(app端)" + " " + goods_id);
        List<Map> mapList = goodsService.queryGoodByIdApp(goods_id);
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }

    @RequestMapping(value = "/queryGoodSpecById",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "根据商品id查询该商品的规格信息(app端接口)",notes = "根据商品id查询该商品的规格信息",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),

            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id",required = true,paramType = "query",dataType = "Integer")
    })
    @ResponseBody
    public ResultDate queryGoodSpecById(Integer goods_id){
        ResultDate resultDate = new ResultDate();
        System.out.println("根据商品id查询该商品的规格信息(app端接口)" + " " + goods_id);
        List<Map> mapList = goodsService.queryGoodSpecById(goods_id);
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/queryGoodImgType",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品图片类型查询",notes = "查询商品图片类型接口",httpMethod = "GET", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ResponseBody
    public ResultDate queryGoodImgType(){
        ResultDate resultDate = new ResultDate();
        System.out.println("查询商品图片类型名称接口");
        List<Map> mapList = goodsService.queryGoodImgType();
        if (mapList.size() > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/addGoods",method = RequestMethod.POST,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品新增",notes = "新增商品接口",httpMethod = "POST", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchant_id", value = "商家id",required = true),
            @ApiImplicitParam(name = "goods_name", value = "商品名称",required = true),
            @ApiImplicitParam(name = "goods_shelves_id", value = "商品上架状态id",required = true),
            @ApiImplicitParam(name = "goods_describe", value = "商品描述",required = true),
            @ApiImplicitParam(name = "goods_home_img_url", value = "商品首页大图",required = true),

            @ApiImplicitParam(name = "goods_original_price", value = "商品原价",required = true),
            @ApiImplicitParam(name = "goods_promotion", value = "商品促销价",required = false),
            @ApiImplicitParam(name = "goods_cost_price", value = "商品成本价",required = true),
            @ApiImplicitParam(name = "goods_courier_price", value = "商品快递价",required = false),
            @ApiImplicitParam(name = "goods_buy_restrictions", value = "商品购买数量限制",required = false),
            @ApiImplicitParam(name = "goods_inventory", value = "商品库存",required = true),

            @ApiImplicitParam(name = "goodsDetailImgList", value = "商品详情页大图集合",required = true)
    })
    @ResponseBody
    public ResultDate addGoods(@RequestBody Map map){
        ResultDate resultDate = new ResultDate();
        System.out.println("添加商品接口" + " " + map);
        int result = goodsService.addGoods(map);
        if (result > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("添加成功");
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/uploadGoodsHomeImg",method = RequestMethod.POST,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品首页大图文件上传",notes = "图片文件地址解析返回",httpMethod = "POST", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="上传成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "商品首页大图文件",required = true)
    })
    @ResponseBody
    public ResultDate uploadGoodsHomeImg(Goods goods,HttpServletRequest request) throws IOException, FileUploadException {
        ResultDate resultDate = new ResultDate();
        System.out.println("商品首页大图上传" + " " + goods.getFile());
        if (goods.getFile() != null){
            Map map = goodsService.uploadGoodsHomeImg(goods,request);
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("图片解析成功");
            resultDate.setData(map);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
    @RequestMapping(value = "/uploadGoodsImgUrl",method = RequestMethod.POST,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品详情页上传",notes = "详情页图片文件地址解析返回",httpMethod = "POST", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="上传成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileArrays", value = "商品详情页图片文件集合",required = true)
    })
    @ResponseBody
    public ResultDate uploadGoodsImgUrl(MultipartFile[] fileArrays,HttpServletRequest request) throws IOException, FileUploadException {
        ResultDate resultDate = new ResultDate();
        System.out.println("商品详情页上传" + " " + Arrays.toString(fileArrays));
        if (fileArrays.length > 0){
            String[] str = goodsService.uploadGoodsImgUrl(fileArrays,request);
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("图片解析成功");
            resultDate.setData(str);
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("未上传任何图片");
        }
        return resultDate;
    }
    @RequestMapping(value = "/editGoods",method = RequestMethod.POST,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品编辑",notes = "根据商品id编辑对应的商品信息",httpMethod = "POST", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="编辑成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gooods_id", value = "商品id",required = true),
            @ApiImplicitParam(name = "goods_name", value = "商品名称",required = true),
            @ApiImplicitParam(name = "goods_shelves_id", value = "商品上架状态id",required = true),
            @ApiImplicitParam(name = "goods_describe", value = "商品描述",required = true),
            @ApiImplicitParam(name = "goods_home_img_url", value = "商品首页大图",required = true),

            @ApiImplicitParam(name = "goods_original_price", value = "商品原价",required = true),
            @ApiImplicitParam(name = "goods_promotion", value = "商品促销价",required = false),
            @ApiImplicitParam(name = "goods_cost_price", value = "商品成本价",required = true),
            @ApiImplicitParam(name = "goods_courier_price", value = "商品快递价",required = false),
            @ApiImplicitParam(name = "goods_buy_restrictions", value = "商品购买数量限制",required = false),
            @ApiImplicitParam(name = "goods_inventory", value = "商品库存",required = true),

            @ApiImplicitParam(name = "goodsDetailImgList", value = "商品详情页大图集合",required = true)
    })
    @ResponseBody
    public ResultDate editGoods(@RequestBody Map map){
        ResultDate resultDate = new ResultDate();
        System.out.println("根据商品id编辑对应的商品信息" + " " + map);
        int result = goodsService.editGoods(map);
        if (result > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("编辑成功");
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }

    @RequestMapping(value = "/deleGoods",method = RequestMethod.POST,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "商品下架功能接口",notes = "根据商品id下架对应的商品（可多个商品操作）",httpMethod = "POST", response = ResultDate.class)
    @ApiResponses({
            @ApiResponse(code=200,message="操作成功"),
            @ApiResponse(code=500,message="系统开小差了")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsList", value = "商品id集合",required = true)
    })
    @ResponseBody
    public ResultDate deleGoods(@RequestBody Integer[] goodsList){
        ResultDate resultDate = new ResultDate();
        System.out.println("根据商品id下架对应的商品（可多个商品操作）" + " " + goodsList);
        int result = goodsService.deleGoods(goodsList);
        if (result > 0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("编辑成功");
        } else{
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("系统开小差了");
        }
        return resultDate;
    }
}
